package forms;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import elements.Account;

@SuppressWarnings("serial")
public class FormSignup extends JDialog{
	class Capchar extends JLabel{
		private String capchar=null;
		public Capchar(){
			super();
			StringBuffer sb=new StringBuffer();
			Random rd=new Random();
			for(int i=0;i<6;i++)sb.append((char)('A'+rd.nextInt('Z'-'A')));
			capchar=sb.toString();
			this.setFont(new Font("Tahoma",Font.BOLD|Font.ITALIC,15));
			this.setText(capchar);
		}
		
	}
	private JTextField txtUser=new JTextField(11);
	private JTextField txtName=new JTextField(11);
	private JTextField txtPass=new JPasswordField(11);
	private JTextField txtConfirm=new JPasswordField(11);
	private FormStart frmStart=null;
	private JButton btCreate=new JButton("Create now");
	private boolean checkVaid(){
		if(txtUser.getText().isEmpty()){
			JOptionPane.showMessageDialog(this,"Bạn phải nhập nickname vào.");
			return false;
		}
		if(txtPass.getText().isEmpty()){
			JOptionPane.showMessageDialog(this,"Bạn phải nhập mật khẩu vào.");
			return false;
		}
		if(!txtPass.getText().equals(txtConfirm.getText())){
			JOptionPane.showMessageDialog(this,"Hai trư�?ng mật khẩu không khớp.");
			return false;
		}
		if(txtName.getText().isEmpty()){
			JOptionPane.showMessageDialog(this,"Bạn phải nhập tên thật vào.");
			return false;
		}
		return true;
	}
	private void loadGui(){
		JPanel panelMain=new JPanel();
		JPanel panelTitle=new JPanel();
		JPanel panelButton=new JPanel();
		JPanel p1=new JPanel(new GridLayout(1,2));
		JPanel p2=new JPanel(new GridLayout(1,2));
		JPanel p3=new JPanel(new GridLayout(1,2));
		JPanel p4=new JPanel(new GridLayout(1,2));
		JPanel p5=new JPanel(new GridLayout(1,2));
		
		panelTitle.add(new JLabel("Create a new acount"));
		panelMain.setBorder(BorderFactory.createTitledBorder("Your information"));
		
		p1.add(new JLabel("User name"));
		p1.add(txtUser);
		p2.add(new JLabel("Your Password"));
		p2.add(txtPass);
		p3.add(new JLabel("Confirm"));
		p3.add(txtConfirm);
		p4.add(new JLabel("Your name"));
		p4.add(txtName);
		p5.add(new Capchar());
		p5.add(new JTextField(10));
		panelMain.add(p1);
		panelMain.add(p2);
		panelMain.add(p3);
		panelMain.add(p4);
		panelMain.add(new JLabel("Enter capchar (you are a human)"));
		panelMain.add(p5);
		panelButton.add(btCreate);
		add(panelTitle,BorderLayout.NORTH);
		add(panelMain,BorderLayout.CENTER);
		add(panelButton,BorderLayout.SOUTH);
		setSize(300,300);
		setLocation(370,150);
		this.setModal(true);
		this.setTitle("Create new acoount");
		btCreate.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				if(!checkVaid())return;
				Signup ps=new Signup(new Account(txtUser.getText(),txtName.getText(),txtPass.getText()));
				ps.setVisible(true);
				if(ps.success){
					frmStart.setUserPassword(txtUser.getText(), txtPass.getText());
				}
			}
		});
	}
	public FormSignup(FormStart _formStart){
		super();
		this.frmStart=_formStart;
		loadGui();
	}
}
